package base.display.activity;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import base.utils.Logger;

public abstract class BaseViewHolder {

	/**
	 * 日志标识
	 */
	protected final String TAG = this.getClass().getSimpleName();

	private Handler pHander;

	/**
	 * The activity.
	 */
	private Activity pActivity;

	/**
	 * Root view of the adv.
	 */
	private View pRootView;

	/**
	 * The context.
	 */
	private Context pContext;

	private Fragment pFragment;

	/**
	 * The full function viewHolder.
	 * 
	 * @param view
	 * @param fragment
	 */
	public BaseViewHolder(View view, Fragment fragment) {
		if (view == null) {
			if (Logger.isLoggingEnabled())
				Logger.error(TAG, "\t view == null ,return ...");
			return;
		}
		pFragment = fragment;
		pRootView = view;
		pContext = view.getContext();
		if (pContext instanceof Activity) {
			pActivity = (Activity) pContext;
		}
	}

	/**
	 * 
	 * @param content
	 * @param layoutId
	 * @param fragment
	 */
	public BaseViewHolder(Context content, int layoutId, Fragment fragment) {
		this(LayoutInflater.from(content).inflate(layoutId, null), fragment);
	}

	/**
	 * 
	 * @param view
	 */
	public BaseViewHolder(View view) {
		this(view, null);
	}

	/**
	 * 
	 * @param context
	 *            Context.
	 * @param layoutId
	 *            The view for the viewHolder's layoutId.
	 */
	public BaseViewHolder(Context context, int layoutId) {
		this(LayoutInflater.from(context).inflate(layoutId, null), null);
	}

	/**
	 * Get the context attached with the viewHolder.
	 * 
	 * @return
	 */
	public Context getContext() {
		return pContext;
	}

	/**
	 * Get the activity attached with the viewHolder.
	 * 
	 * @return
	 */
	public Activity getActivity() {
		return pActivity;
	}

	/**
	 * Get the fragment ,you must user the {@link #ViewHolderBaseClass(View, Fragment)} .
	 * 
	 * @return
	 */
	public Fragment getFragment() {
		return pFragment;
	}

	/**
	 * Get the root view of the viewHolder.
	 * 
	 * @return
	 */
	public View getRootView() {
		return pRootView;
	}

	public Resources getResources() {
		return getContext() == null ? null : getContext().getResources();
	}

	public void showView() {
		pRootView.setVisibility(View.VISIBLE);
	}

	public void hideView() {
		pRootView.setVisibility(View.GONE);
	}

	public boolean isVisiable() {
		return pRootView != null && pRootView.getVisibility() == View.VISIBLE;
	}

	public Handler getHandler() {
		if (pHander == null) {
			pHander = new Handler();
		}
		return pHander;
	}

	/**
	 * Initiate the view.<br>
	 * Attention that Only if you use the proper constructor would the data passed to this method.
	 * 
	 */
	public abstract <T> void initView(T data);

	/**
	 * Updating view
	 * 
	 * @param data
	 */
	public <T> void updateView(T data) {

	}

	public void clear() {

	}

}
